class TermController {
    def timeUtilService
    static allowedMethods = [save: "POST", update: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        params.order= "desc"
        params.sort= "id"
 
        if (session.school) {
            [termInstanceList: Term.findAllBySchool(session.school , params), termInstanceTotal: Term.countBySchool(session.school)]  
        }else{
            [termInstanceList: [], termInstanceTotal: 0]  
        }

    }

    def create = {
        def termInstance = new Term()
        termInstance.term = Term.currentTerm()

        

        if (!Term.withCriteria {
                and{
                    eq('term' , termInstance.term) 
                    eq('year' ,  termInstance.year ) 
                    eq('school' , session.school) 
                }   
            }) { 
            return [termInstance: termInstance]
        }else{
            flash.message = "${termInstance}${message(code: 'term.label', default: 'Term')}已经存在，不需要再次添加。"
            redirect(action: "list", params: params)
        }
    }

    def save = {
         params.startTime =  timeUtilService.stringToDate(params.startTime)
         params.endTime =  timeUtilService.stringToDate(params.endTime)
         def termInstance = new Term(params)
        // 检查时间是否有冲突
        def conflict = Term.withCriteria{
            and {
                or{
                    between('startTime' , params.startTime ,params.endTime)
                    between('endTime' , params.startTime ,params.endTime)
                }
                eq('school' , session.school)
            }
            maxResults(1)
        }
        if (conflict) {
            flash.message = "[${termInstance}]教学计划和[${conflict}]教学计划时间上有重叠！请调整时间。"
            render(view: "create", model: [termInstance: termInstance])
        }
 
        termInstance.school = session.school
        if (termInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'term.label', default: 'Term'), termInstance])}"
            redirect(action: "list")
        }
        else {
            render(view: "create", model: [termInstance: termInstance])
        }
    }

    def show = {
        def termInstance = Term.get(params.id)
        if (!termInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'term.label', default: 'Term'), params.id])}"
            redirect(action: "list")
        }
        else {
            [termInstance: termInstance]
        }
    }

    def edit = {
        def termInstance = Term.get(params.id)
        if (!termInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'term.label', default: 'Term'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [termInstance: termInstance]
        }
    }

    def update = {

        def termInstance = Term.get(params.id)
        if (termInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (termInstance.version > version) {
                    
                    termInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'term.label', default: 'Term')] as Object[], "Another user has updated this Term while you were editing")
                    render(view: "edit", model: [termInstance: termInstance])
                    return
                }
            }
             params.startTime =  timeUtilService.stringToDate(params.startTime)
             params.endTime =  timeUtilService.stringToDate(params.endTime)
             termInstance.properties = params
            if (!termInstance.hasErrors() && termInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'term.label', default: 'Term'), termInstance])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [termInstance: termInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'term.label', default: 'Term'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def termInstance = Term.get(params.id)
        if (termInstance) {
            try {
                termInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'term.label', default: 'Term'), termInstance])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'term.label', default: 'Term'), termInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'term.label', default: 'Term'), params.id])}"
            redirect(action: "list")
        }
    }
}
